package com.zadtek.sharkRider.receiver;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import com.zadtek.sharkRider.R;
import com.zadtek.sharkRider.activity.MainActivity;

import java.util.zip.Adler32;

public class NotificationHelper {

    @SuppressWarnings("deprecation")
    public static void showMessageNotification(
    		Context context, NotificationManager nm,
    		String title, String msgContent, String channel, 
    		Bundle bundle, String type) {

        boolean isChannel = !isEmpty(channel);
        String chatting = null;
        if (isChannel) {
            chatting = channel;
        } else {
            chatting = title;
        }

        Intent select = new Intent();
        select.setClass(context, MainActivity.class);
//        select.putExtra("chatting", chatting);
//        select.putExtra("isChannel", isChannel);
//        select.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        select.putExtras(bundle);
        select.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        
//        Notification notification = new Notification(R.drawable.icon, msgContent, System.currentTimeMillis());
//        if (!isEmpty(channel)) {
//            title = title + " (" + channel + ")";
//        }
        
        int notificationId = getNofiticationID(title);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, select, PendingIntent.FLAG_UPDATE_CURRENT);
//        notification.setLatestEventInfo(context, title, msgContent, contentIntent);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setAutoCancel(true)
//                .setDefaults(Notification.DEFAULT_ALL)
//                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.icon)
                .setTicker("Hearty365")
                .setContentTitle("通知标题")
                .setContentText("我是消息")
//                .setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
                .setContentIntent(contentIntent);
//                .setContentInfo("Info");


        showMessageNotificationLocal(context, nm, builder, notificationId, type);
    }

    private static void showMessageNotificationLocal(Context context, NotificationManager nm, NotificationCompat.Builder notification, int notificationId, String type) {
//        notification.ledARGB = 0xff00ff00;
//        notification.ledOnMS = 300;
//        notification.ledOffMS = 1000;
//        notification.flags |= Notification.FLAG_SHOW_LIGHTS;
//        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        
//        boolean needSound = true;
//        boolean needVibrate = true;
        
//    	if (needSound && needVibrate) {
//        	notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
//        } else if (needSound){
//        	notification.defaults = Notification.DEFAULT_SOUND;
//        } else if (needVibrate) {
//        	notification.defaults = Notification.DEFAULT_VIBRATE;
//        }
    	
    	if(type.equals("at")) {
			//设置声音,自定义声音(有新的订单)
			notification.setSound(Uri.parse("android.resource://" + context.getPackageName()
					+ "/" + R.raw.push_to_order));
			notification.setDefaults(Notification.DEFAULT_VIBRATE);
		} else if(type.equals("pm")) {
			//设置声音,自定义声音(系统派单消息)
			notification.setSound(Uri.parse("android.resource://" + context.getPackageName()
                    + "/" + R.raw.push_by_system));
			notification.setDefaults(Notification.DEFAULT_VIBRATE);
		} else {
			//系统默认声音
			notification.setDefaults(Notification.DEFAULT_SOUND);
			notification.setDefaults(Notification.DEFAULT_VIBRATE);
		}
    	
        nm.notify(notificationId, notification.build());
    }
    
    public static int getNofiticationID(String friend) {
        if (TextUtils.isEmpty(friend)) {
            return 0;
        }
        
        int nId = 0;
        Adler32 adler32 = new Adler32();
        adler32.update(friend.getBytes());
        nId = (int) adler32.getValue();
        if (nId < 0) {
            nId = Math.abs(nId);
        }
        
        if (nId < 0) {
            nId = Math.abs(nId);
        }
        return nId;
    }
    
    
    public static boolean isEmpty(String s) {
        if (null == s) return true;
        if (s.length() == 0) return true;
        if (s.trim().length() == 0) return true;
        return false;
    }


}
